package com.ydzt.fanka.server.domain;

import java.io.Serializable;

import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.Pointer;
import org.xvolks.jnative.pointers.memory.MemoryBlockFactory;

public class MsgParam implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/*
	 * 各项参数总长度
	 */
	public static final int DEV_TYPE_LEN = 1;
	public static final int DEV_ID_LEN = 2;
	public static final int ALGO_TYPE_LEN = 1;
	public static final int PROTOCOL_VER_LEN = 1;
	public static final int PARA_MAX_CMD_LEN = 1049;
	
	public static final int MP_ALL_LEN = 1054;
	
	
	
	/*
	 * 各项参数的起始位置
	 */
	public static final int START_DEV_TYPE = 0;
	public static final int START_DEV_ID = 1;
	public static final int START_ALGO_TYPE = 3;
	public static final int START_PROTOCOL_VER = 4;
	public static final int START_PARA = 5;
	
	private Pointer pointer;//申明指针
	private String devType;
	private short devID;
	private String algoType;
	private String protocolVer;
	private String para;
	
	public void createPointer() throws NativeException{
		this.pointer = new Pointer(MemoryBlockFactory.createMemoryBlock(getSizeOf()));
	}
	
	public int getSizeOf(){
		return MP_ALL_LEN;
	}
	
	public void setDevType(String devType) throws NativeException{
		if(devType == null || devType.getBytes().length > DEV_TYPE_LEN){
			throw new NativeException("传入设备类型,参数长度错误");
		}
		this.devType = devType;
	}
	
	public String getDevType() throws NativeException{
		return this.devType;
	}
	
	public void setDevID(short devID) throws NativeException{
		this.devID = devID;
	}
	
	public short getDevID() throws NativeException{
		return this.devID;
	}
	
	public void setAlgoType(String algoType) throws NativeException{
		if(algoType == null || algoType.getBytes().length > ALGO_TYPE_LEN){
			throw new NativeException("传入算法类型,参数长度错误");
		}
		this.algoType = algoType;
	}
	
	public String getAlgoType() throws NativeException{
		return this.algoType;
	}
	
	public void setProtocolVer(String protocolVer) throws NativeException{
		if(protocolVer == null || protocolVer.getBytes().length > PROTOCOL_VER_LEN){
			throw new NativeException("传入协议版本,参数长度错误");
		}
		this.protocolVer = protocolVer;
	}
	
	public String getProtocolVer() throws NativeException{
		return this.protocolVer;
	}
	
	public void setPara(String para) throws NativeException{
		if(para == null || para.getBytes().length > PARA_MAX_CMD_LEN){
			throw new NativeException("传入参数,参数长度错误");
		}
		this.para = para;
	}
	
	public String getPara() throws NativeException{
		return this.para;
	}
	
	public Pointer getPointer() throws NativeException{
		if(this.pointer == null){
			this.createPointer();
		}
		//设置设备类型
		this.pointer.setStringAt(START_DEV_TYPE, this.devType);
		//设置设备ID
		this.pointer.setShortAt(START_DEV_ID, devID);
		//设置算法类型
		this.pointer.setStringAt(START_ALGO_TYPE, algoType);
		//设置协议版本
		this.pointer.setStringAt(PROTOCOL_VER_LEN, protocolVer);
		//设置传入参数
		this.pointer.setStringAt(START_PARA, para);
		return this.pointer;
	}
}
